package crm.model.teacherPC.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import crm.model.Setting;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.bannedList.ApplicationBanned;
import crm.model.teacherPC.bannedList.BrowserBanned;
import crm.model.teacherPC.connectList.ConnectStudent;
import crm.model.teacherPC.net.Transfer;



public class XMLWriter {
	private final Manager manager;
	private final BrowserBanned browserBannedList;
	private final ApplicationBanned appBannList;
	public XMLWriter(Manager manager,  BrowserBanned browserBannedList, ApplicationBanned appBannList) {
		this.manager=manager;
		this.browserBannedList = browserBannedList;
		this.appBannList=appBannList;
	}
	public void save() {

		try {
			File f = Setting.getDataFile();
			if (!f.exists()){
				f.createNewFile();
			}
			XMLOutputFactory factory = XMLOutputFactory.newInstance();
			XMLStreamWriter writer = factory
					.createXMLStreamWriter(new FileOutputStream(f));

			writer.writeStartDocument();
			{
				writer.writeStartElement("JCRM");
				{
					for(ConnectStudent c: manager.connectingStudent.values()){
						writer.writeStartElement("Student");
						writer.writeAttribute("IP", c.getIP());
						writer.writeAttribute("Info", c.getInfo());
						writer.writeEndElement();
					}
					for (String filter: Transfer.filterList){
						writer.writeStartElement("Filter");
						writer.writeAttribute("Value", filter);
						writer.writeEndElement();
					}
					for (int i = 0; i < browserBannedList.getNOfBannedBrowsers(); i++) {
						writer.writeStartElement("Browser");
						writer.writeAttribute("Name",browserBannedList.getBannedBrowser(i).name);
						writer.writeAttribute("enable",""+browserBannedList.getBannedBrowser(i).getEnable());
						
						writer.writeEndElement();
					}
					for (int i = 0; i < appBannList.getNOfBann(); i++) {
						writer.writeStartElement("Application");
						writer.writeAttribute("Name",appBannList.getBannedBrowser(i).name);
						writer.writeAttribute("enable",""+appBannList.getBannedBrowser(i).getEnable());
						
						writer.writeEndElement();
					}

				}
				writer.writeEndElement();
			}
			writer.writeEndDocument();

			writer.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (XMLStreamException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}
